/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef TURBO_TRANS_NAPI_UTIL_H
#define TURBO_TRANS_NAPI_UTIL_H

#include "napi/native_api.h"
#include "logger.h"
#include <cstdint>
#include <string>
#define CHECK_NAPI_STATUS(cond, message, ...)                                                                          \
  do {                                                                                                                 \
    napi_status _status = (cond);                                                                                      \
    if (_status != napi_ok) {                                                                                          \
      LOGE(message, ##__VA_ARGS__);                                                                                    \
      return _status;                                                                                                  \
    }                                                                                                                  \
  } while (0)


#define CHECK_COND(cond, res, message, ...)                                                                            \
  do {                                                                                                                 \
    if (!(cond)) {                                                                                                     \
      LOGE(message, ##__VA_ARGS__);                                                                                      \
      return res;                                                                                                      \
    }                                                                                                                  \
  } while (0)


class NapiUtil {
  public:
  static napi_status GetDouble(napi_env env, napi_value arg, double *value);
  /**
   *  使用double获取，转换为float
   * @param env
   * @param arg
   * @param value
   * @return
   */
  static napi_status GetFloat(napi_env env, napi_value arg, float *value);
  static napi_status GetInt32(napi_env env, napi_value arg, int32_t *value);
  static napi_status GetInt64(napi_env env, napi_value arg, int64_t *value);
  static napi_status GetUInt32(napi_env env, napi_value arg, uint32_t *value);
  static napi_status GetUInt64(napi_env env, napi_value arg, uint64_t *value);
  static napi_status GetBool(napi_env env, napi_value arg, bool *value);
  static napi_status GetStringUtf8(napi_env env, napi_value arg, std::string *value);
  static napi_status GetUint8Array(napi_env env, napi_value arg, size_t *length, void **data, napi_value *arraybuffer,
                                   size_t *byte_offset);
  static bool CheckObject(napi_env env, napi_value arg);
};

#endif // TURBO_TRANS_NAPI_UTIL_H
